package test.controller;

import JDBC.InputUtil;
import test.VO.Article;
import test.mapper.ArticleMapper;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ArticleController {

    private ArticleMapper articleMapper = new ArticleMapper();

    public static void main(String[] args) {
        ArticleController articleController = new ArticleController();
        articleController.start();
    }

    public void start() {
        a:while (true) {
            System.out.println("欢迎来到文章管理系统");
            System.out.println("请输入操作编号");
            System.out.println("1:保存文章");
            System.out.println("2:删除文章操作");
            System.out.println("3:查询指定文章操作");
            System.out.println("4:查询所有文章操作");
            System.out.println("5:再见");

            Scanner scanner = new Scanner(System.in);
            String number = scanner.nextLine();

            switch (number) {
                case "1":
                    writeArticle();
                    break;
                case "2":
                    WriteDelete();
                    break;
                case "3" :
                    WriteFindById();
                    break;
                case "4":
                    WriteFindAll();
                    break;
                case "5":
                    System.out.println("再见");
//                    return;
                    break a;
                default:
                    System.out.println("编号输入有误");
            }
        }
    }
    public void writeArticle() {

        Article article = InputUtil.getInputObject(new Article(), "发表文章");
        //调用ArticleMapper将文章保存

        int num = articleMapper.insertArticle(article);
        if (num > 0) {
            System.out.println("成功");
        } else {
            System.out.println("失败");
        }
    }

    public void WriteDelete() {
        System.out.println("删除文章");
        System.out.println("请输入要删除的id");

        Scanner scanner = new Scanner(System.in);
        int id = Integer.parseInt(scanner.nextLine());

        int num = articleMapper.deleteArticleById(id);

        System.out.println(num > 0 ? "删除成功" : "删除失败");
//        if (id>0){
//            System.out.println("删除成功");
//        }else {
//            System.out.println("删除失败");
//        }
    }

    public void WriteFindById() {
        System.out.println("查询文章");
        System.out.println("请输入要查询的文章id");
        Scanner scanner = new Scanner(System.in);
        int id = Integer.parseInt(scanner.nextLine());
        Article article = articleMapper.findById(id);
        System.out.println("标题:"+article.getTitle());
        System.out.println("正文:"+article.getContent());
    }

    public void WriteFindAll() {
        List<Article> articles = articleMapper.findAll();
        for(Article article : articles){
            System.out.println("标题:"+article.getTitle());
            System.out.println("正文:"+article.getContent());
            System.out.println();
        }


    }


}
